package com.bjqh.edu.UI.Activity.StudentPersonal;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bjqh.edu.Base.AppConstants;
import com.bjqh.edu.Bean.Room;
import com.bjqh.edu.R;
import com.bjqh.edu.UI.Activity.Index.VideoActivity;
import com.bjqh.edu.adapter.PendingAdapter;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.cache.CacheMode;
import com.lzy.okgo.callback.StringCallback;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;
import okhttp3.Response;

public class StuCumulativeClassActivity extends ListActivity implements SwipeRefreshLayout.OnRefreshListener {

    @BindView(android.R.id.list)
    ListView list;
    @BindView(R.id.swipeLayout)
    SwipeRefreshLayout swipeLayout;
    @BindView(android.R.id.empty)
    LinearLayout empty;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_qx)
    TextView tvQx;
    @BindView(R.id.rl_top_tilte)
    RelativeLayout rlTopTilte;

    private PendingAdapter adapter;
    private List<Room.DataBean> dataBeen;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //全屏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_stu_cumulative_class);
        ButterKnife.bind(this);
        list.setEmptyView(empty);
        swipeLayout.setColorSchemeResources(R.color.grean);
        swipeLayout.setOnRefreshListener(this);
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        tvTitle.setText("累计上课");

        getValue();
    }

    private void getValue() {
        OkGo.post(AppConstants.BASE_URL + "/rtu/myRoom.shtml")
                .tag(this)
                .cacheKey("cacheKey")
                .cacheMode(CacheMode.DEFAULT)
                .params("status", 5)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        swipeLayout.setRefreshing(false);
                        Log.i("完成课程", s);
                        Gson gson = new Gson();
                        Room room = gson.fromJson(s, Room.class);
                        if (room.getMsg() != null) {
                            if (room.getMsg().equals("timeout")) {
                                AppConstants.gotoLogin(StuCumulativeClassActivity.this);
                            } else if (room.getMsg().equals("remote login")) {
                                AppConstants.remologin(StuCumulativeClassActivity.this);
                            }
                        } else {
                            dataBeen = room.getData();
                            if (dataBeen != null) {
                                adapter = new PendingAdapter(StuCumulativeClassActivity.this, dataBeen);
                                list.setAdapter(adapter);
                            }

                        }


                    }

                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        super.onError(call, response, e);
                        Toast.makeText(StuCumulativeClassActivity.this, "报告小主，服务器被外星人偷走啦~~", Toast.LENGTH_SHORT).show();
                    }
                });
    }
    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        Long aaa=dataBeen.get(position).getRid();
        if(aaa!=null)
        {
            Intent intent = new Intent(StuCumulativeClassActivity.this, VideoActivity.class);
            intent.putExtra("rid", dataBeen.get(position).getRid());
            // intent.putExtra("name", dataBeen.get(i).getName());
           startActivity(intent);
        }

    }

    @Override
    public void onRefresh() {
getValue();
    }
}
